/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.NetworkManagement.Components;

import Beans.VirtualResourceBean;
import com.sun.xml.ws.commons.virtualbox_3_2.IHostNetworkInterface;
import org.virtualbox_3_2.HostNetworkInterfaceMediumType;
import org.virtualbox_3_2.HostNetworkInterfaceStatus;
import org.virtualbox_3_2.HostNetworkInterfaceType;

/**
 * This class represents the host network interface configuration.
 *
 * @author Angel Sanadinov
 */
public class HostNetworkConfiguration extends NetworkManagementComponent
{
    private String interfaceName;       //the name of the network interface
    private String networkName;         //the name of the network
    private boolean isDHCPEnabled;      //denotes whether a DHCP server is available
    private String IPv4Address;         //the IPv4 address of the interface
    private String IPv4Mask;            //the IPv4 mask of the interface
    private String IPv6Address;         //the IPv6 address of the interface
    private long IPv6MaskPrefixLength;  //the IPv6 mask prefix length
    private boolean isIPv6Supported;    //denotes whether IPv6 is supported
    private String hardwareAddress;     //the physical interface address
    private HostNetworkInterfaceMediumType protocolEncapsulation; //type of network medium
    private HostNetworkInterfaceStatus interfaceStatus; //the network interface status
    private HostNetworkInterfaceType interfaceType; //the type of network interface

    private DHCPServerConfiguration dhcpServer; //DHCP server data
    private VirtualResourceBean networkData;    //network data from the database

    /**
     * Constructs host network interface configuration using the specified data. <br><br>
     *
     * Used for retrieving data for existing host interfaces.
     *
     * @param networkInterface the host interface, from which the data will be retrieved
     * @param dhcpServer DHCP server configuration for the interface, if available
     * @param networkData the virtual resource network data
     */
    public HostNetworkConfiguration(IHostNetworkInterface networkInterface,
                                    DHCPServerConfiguration dhcpServer,
                                    VirtualResourceBean networkData)
    {
        this.interfaceName = networkInterface.getName();
        this.networkName = networkInterface.getNetworkName();
        this.isDHCPEnabled = networkInterface.getDhcpEnabled();
        this.IPv4Address = networkInterface.getIPAddress();
        this.IPv4Mask = networkInterface.getNetworkMask();
        this.IPv6Address = networkInterface.getIPV6Address();
        this.IPv6MaskPrefixLength = networkInterface.getIPV6NetworkMaskPrefixLength();
        this.isIPv6Supported = networkInterface.getIPV6Supported();
        this.hardwareAddress = networkInterface.getHardwareAddress();
        this.protocolEncapsulation = networkInterface.getMediumType();
        this.interfaceStatus = networkInterface.getStatus();
        this.interfaceType = networkInterface.getInterfaceType();
        this.dhcpServer = dhcpServer;
        this.networkData = networkData;
    }

    /**
     * Retrieves the name of the network interface.
     *
     * @return the interface name
     */
    public String getInterfaceName()
    {
        return interfaceName;
    }

    /**
     * Retrieves the name of the network, associated with this interface.
     *
     * @return the network name
     */
    public String getNetworkName()
    {
        return networkName;
    }

    /**
     * Returns the DHCP server of the network. <br>
     *
     * <b>Note:</b> <i>This denotes whether an external DHCP server is available
     * for the host interface and not if a VirtualBox DHCP server was created for it.</i>
     *
     * @return <code>true</code> if a DHCP is enabled or <code>false</code>
     *         if not
     */
    public boolean isDHCPEnabled()
    {
        return isDHCPEnabled;
    }

    /**
     * Returns whether a DHCP server is available or not.
     *
     * @return <code>true</code> if a DHCP server is available or <code>false</code>
     *         otherwise
     */
    public boolean isDHCPAvailable()
    {
        if(dhcpServer != null)
            return true;
        else
            return false;
    }

    /**
     * Retrieves the IPv4 address of the interface.
     *
     * @return the IPv4 address
     */
    public String getIPv4Address()
    {
        return IPv4Address;
    }

    /**
     * Retrieves the IPv4 network mask of the interface.
     *
     * @return the IPv4 mask
     */
    public String getIPv4Mask()
    {
        return IPv4Mask;
    }

    /**
     * Retrieves the IPv6 address of the interface.
     *
     * @return the IPv6 address
     */
    public String getIPv6Address()
    {
        return IPv6Address;
    }

    /**
     * Retrieves the IPv6 network mask prefix length.
     *
     * @return the IPv6 mask prefix length
     */
    public long getIPv6MaskPrefixLength()
    {
        return IPv6MaskPrefixLength;
    }

    /**
     * Returns whether IPv6 is supported by the interface.
     *
     * @return <code>true</code> if IPv6 is supported or <code>false</code> if it is not
     */
    public boolean isIPv6Supported()
    {
        return isIPv6Supported;
    }

    /**
     * Retrieves the hardware address of the interface.
     *
     * @return the hardware address
     */
    public String getHardwareAddress()
    {
        return hardwareAddress;
    }

    /**
     * Retrieves the protocol encapsulation used by the interface.
     *
     * @return the type protocol encapsulation
     */
    public HostNetworkInterfaceMediumType getProtocolEncapsulation()
    {
        return protocolEncapsulation;
    }

    /**
     * Retrieves the status of the network interface.
     *
     * @return the interface status
     */
    public HostNetworkInterfaceStatus getInterfaceStatus()
    {
        return interfaceStatus;
    }

    /**
     * Retrieves the type of the host network interface.
     *
     * @return the interface type
     */
    public HostNetworkInterfaceType getInterfaceType()
    {
        return interfaceType;
    }

    /**
     * Retrieves the DHCP server data.
     *
     * @return the DHCP server configuration, if available or <code>null</code> otherwise
     */
    public DHCPServerConfiguration getDhcpServer()
    {
        return dhcpServer;
    }

    /**
     * Retrieves the network data from the database.
     *
     * @return the network data
     */
    public VirtualResourceBean getNetworkData()
    {
        return networkData;
    }

    @Override
    public boolean isValid()
    {
        if(networkData != null && networkData.isValid() && interfaceType != null
                && protocolEncapsulation != null && interfaceStatus != null
                && interfaceName != null && networkName != null
                && (!isDHCPEnabled || interfaceType == HostNetworkInterfaceType.BRIDGED
                    || (dhcpServer != null && dhcpServer.isValid())))
            return true;
        else
            return false;
    }
}
